package com.stone;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import net.sf.json.JSONObject;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;


public class GfanSpider {
	private static final String GdownurlPattern = "<input id=\"hidDownApkUrl\" type=\"hidden\" value=\"(.+?)\"+ />";
	private static final String GsizePattern = "大小：<span class=\"d_i_c\">(.+?)M</span>";
	private static final String GnamePattern = "<div class=\"app_name\">(.+?)<span class=\"v\">";
	private static final String GauthorPattern = "<span class=\"i i2\">作者：<span class=\"d_i_c\">(.+?)</span></span>";
	private static final String GsendtimePattern = "<span class=\"i i3\">更新时间：<span class=\"d_i_c\">(.+?)</span></span>";
	private static final String GinfoPattern = "<div class=\"intro\">(.+?)</div>\\s*<h3 class=\"d_title\">应用截图：</h3>";
	private static final String GversionsPattern = "<span class=\"v\">Ver：(.+?)</span>";
	private static final String GpicturesPattern = "<span class=\"imgouter\"><img src=\"(.+?)\" class=\"simg\" alt=\"\"/></span>";
	private static final String GqrcodePattern = "<img alt=\"二维码\" id=\"ProductDetail1_imgB\" src=\"(.+?)\" />";
	private static final String GgamelogPattern = "<span class=\"i icon\"><img src=\"(.+?)\" alt=\"产品图标\" width=\"48\" height=\"48\" style=\"padding: 6px 5px 5px 6px\" /></span>";
	private static int index = 0;
	static String fileString = ""; 
	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception{
		
		
		File file = new File("job/gfan/list");
		File[] files = file.listFiles();
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < files.length; i++) {
			File[] wenjian = files[i].listFiles();
			String title = files[i].getName();
			sb = new StringBuffer();
			for (int j = 0; j < wenjian.length; j++) {
				BufferedReader is = new BufferedReader(new InputStreamReader(
						new FileInputStream(wenjian[j])));
				String str;
				while ((str = is.readLine()) != null) {
					storeItem(str,title);
				}
				is.close();
			}
		}
	}
	
	
	
	private static String storeItem(String urls,String title) {
		// TODO Auto-generated method stub
		
		String url = "http://apk.gfan.com"+urls.replaceAll("\"", "");
		fileString = getFile(url);
		
		GameInfo ginfo = new GameInfo();
		ginfo.setName(getStringRs(GnamePattern));
		ginfo.setSize(getStringRs(GsizePattern));
		ginfo.setDownurl("http://apk.gfan.com"+getStringRs(GdownurlPattern));
		ginfo.setAuthor(getStringRs(GauthorPattern));
		ginfo.setSendtime(getStringRs(GsendtimePattern));
		ginfo.setCategory(title);
		ginfo.setInfo(getStringRs(GinfoPattern));
		ginfo.setVersions(getStringRs(GversionsPattern));
		ginfo.setPictures(getStringRs(GpicturesPattern));
		ginfo.setQrcode(getStringRs(GqrcodePattern));
		ginfo.setSrcurl(url);
		ginfo.setSrcwebsite("http://apk.gfan.com");
		ginfo.setSrcwebsitename("机锋市场");
		ginfo.setGamelogo(getStringRs(GgamelogPattern));
		//保存到数据库
		storeDb(ginfo);
		storeFile(ginfo,title);
		
		return "";
	}



	private static void storeFile(GameInfo ginfo, String title) {
		JSONObject outString = JSONObject.fromObject(ginfo);

		try {
			File ff = new File("job/gfan/info/"+title);
			if(!ff.exists()){
				ff.mkdirs();
			}
			File f = new File("job/gfan/info/"+title+"/gameInfo"+(index)+".json");
			 BufferedWriter output = new BufferedWriter(new FileWriter(f));
		     output.write(outString.toString());
		     output.close();
		     index++;
		     System.out.println("保存数据文件"+index);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}



	private static void storeDb(GameInfo ginfo) {
		DB db = new DB();
		db.update(ginfo);
		db.close();
		System.out.println("保存到数据库"+index);
	}



	private static String getStringRs(String gnamePattern) {
		// TODO Auto-generated method stub
		Pattern pa = Pattern.compile(gnamePattern);
		Matcher ma = pa.matcher(fileString);
		
		boolean result = ma.find();
		StringBuffer str = new StringBuffer();
		while(result) {
			
			str.append("-!-"+ma.group(1));
			
			//System.out.println(str);
            //继续查找下一个匹配对象 
            result = ma.find();
        }
		
		String s = str.toString();
		if(s.indexOf("-!-")==0){
			s = s.substring(3, s.length());
		}
		return s;
	}



	private static String getFile(String urlString) {
		// TODO Auto-generated method stub
		StringBuffer buffer = new StringBuffer();
		boolean flag = false;
		try {
			URL url = new URL(urlString);
			URLConnection conn = url.openConnection();

			BufferedReader is = new BufferedReader(new InputStreamReader(
					conn.getInputStream()));
			String str;
			while ((str = is.readLine()) != null) {
				buffer.append(str);
			}
			is.close();

		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return buffer.toString();
		
	}
	
	
	
}
